<?php


namespace addons\member\model;

/**
 * 用户关注表
 */
class MemberFocus extends \web\common\model\BaseModel {

    protected function _initialize() {
        $this->tableName = 'member_focus';
    }

    /**
     * 获得关注和粉丝数量
     * @param $user_id
     * @return mixed
     */
    public function getData($user_id){
        $data['focus'] = $this->where(['user_id' => $user_id])->count()??0;
        $data['fans'] = $this->where(['to_user_id' => $user_id])->count()??0;
        return $data;
    }

    /**
     * 添加用户关注
     * @param $user_id
     * @param $to_user_id
     * @return int|\web\common\model\type
     */
    public function getFocus($user_id, $to_user_id){
        //判断本人是否关注过
        $find = $this->where(['user_id' => $user_id, 'to_user_id' => $to_user_id])->find();
        if($find){
            //有的话再此就取消
            $re_id = $this->where(['user_id' => $user_id, 'to_user_id' => $to_user_id])->delete();
            if(!$re_id){
                return 0;
            }
            //删除本人关注他的时候 去查出他是否关注过我  有的话  取消互相关注
            $re_to = $this->isFocus($user_id, $to_user_id);
            if($re_to == 1){
                $re_up = $this->where(['user_id' => $to_user_id, 'to_user_id' => $user_id])->update(['is_focus' => 0]);
                if(!$re_up){
                    return 0;
                }
            }
        }else{
            //查询他是否也关注过本人
            $re = $this->isFocus($user_id, $to_user_id, 0);
//            dump($re);die;
            $is_focus = 0;
            if($re > 0){
                //有的话双方都为互相关注
                $is_focus = 1;
                $re_id = $this->where(['user_id' => $to_user_id, 'to_user_id' => $user_id])->update(['is_focus' => $is_focus]);
                if(!$re_id){
                    return 0;
                }
            }
            //没有的话直接添加本人关注他的记录
            $re_id = $this->add(['user_id' => $user_id, 'to_user_id' => $to_user_id, 'create_time' => date('Y-m-d H:i:s'), 'is_focus' => $is_focus]);
            if(!$re_id){
                return 0;
            }
        }
        return 1;
    }

    /**
     * 对方是否也关注了
     * @param $user_id
     * @param $to_user_id
     * @param int $type  0对方是否关注本人 1=本人是否关注过对方
     * @return int
     */
    public function isFocus($user_id, $to_user_id, $type = 0){
        if($type == 0){
            $find_info = $this->where(['user_id' => $to_user_id, 'to_user_id' => $user_id])->find();
            if($find_info){
                return 1;
            }
            return 0;
        }else{
            $find_info = $this->where(['user_id' => $user_id, 'to_user_id' => $to_user_id])->find();
            if($find_info){
                return 1;
            }
            return 0;
        }
    }


    public function getUserList($pageIndex = -1, $pageSize = -1, $filter = '', $fields ='a.*',  $order = 'id asc') {
        $m = new \addons\member\model\MemberAccountModel();
        $sql = 'select '.$fields.' from '.$this->getTableName().' a left join '.$m->getTableName().' b on a.to_user_id=b.id';

        if (!empty($filter))
            $sql .=  ' where '.$filter ;
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }

    public function getToUserList($pageIndex = -1, $pageSize = -1, $filter = '', $fields ='a.*',  $order = 'id asc') {
        $m = new \addons\member\model\MemberAccountModel();
        $sql = 'select '.$fields.' from '.$this->getTableName().' a left join '.$m->getTableName().' b on a.user_id=b.id';

        if (!empty($filter))
            $sql .=  ' where '.$filter ;
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }




}